package com.lute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.json.ClientNameJSON;
import com.lute.model.Client;
import com.lute.utils.ServerErrorResponse;

@Controller
public class UpdateClientServlet {

	@RequestMapping (value="/updateClient",method= RequestMethod.POST, headers = "Content-type=application/json")
	public @ResponseBody String updateClient(@RequestBody String jsonFromUI, HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		try {
			jsonReq = new JSONObject(jsonFromUI);
			jsonRes  = new JSONObject();
		} catch(JSONException e) {
			jsonRes = new JSONObject();
			jsonRes.put("result", ServerErrorResponse.MALFORMED_REQUEST.toString());
			result = jsonRes.toString();
			return result;
		}
		
		int userId = (Integer)session.getAttribute("sessionUserId");
		String userRole = (String) session.getAttribute("role");
		
		int clientId = Integer.parseInt(jsonReq.getString("clientId"));
		String clientName = jsonReq.getString("clientName");
		
		try {
			// verification
			if (userRole.equals("admin")) {
				Client client = Client.getClientFromDB("id_client", clientId);

				// checking if Client exists
				if (client == null){
					jsonRes.put("result", ServerErrorResponse.USER_NOT_FOUND.toString());
					result = jsonRes.toString();
					System.out.println("User not found");
					return result;
				} else {
					List<ClientNameJSON> clientIdAndNameList = new ArrayList<ClientNameJSON>();
					clientIdAndNameList = Client.getClientsIdAndName();
					
					// checking if Client name is unique
					for (ClientNameJSON clientNameJSON : clientIdAndNameList) {
						if (clientNameJSON.getClientName().equals(clientName)){
							jsonRes.put("result", ServerErrorResponse.CLIENT_NAME_NOT_UNIQUE.toString());
							result = jsonRes.toString();
							System.out.println("Client name not unique");
							return result;
						}
					}
					
					client.setName(clientName);
					Client.updateClientInDB(client);
					jsonRes.put("result", "OK");
					result = jsonRes.toString();
					System.out.println("Client updated");
				}
			} else {
				jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
				result = jsonRes.toString();
				System.out.println("User not entitled");
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
